<template>
	<om-formsub-outer :index="index" :deleteBtn='deleteBtn' :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData,getFormFields, subOnLoad,getEditor,changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import urlUtil from "@/static/js/urlUtil.js"
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				userCacheKey:"obj-month-tmonthlyPlan-work-user-cache",
				user1CacheKey:"obj-month-tmonthlyPlan-work-user1-cache",
				indexCacheKey:"obj-month-tmonthlyPlan-work-index-cache",
				defaultDataCache:'obj-month-tmonthlyPlan-work-user-defaultData-cache',
				intNum:0,
				busCode:'',
				canEdit:true,
				deleteBtn:false
			})
		},
		methods:{
			inputMaxScore(val){
				let _that = this
				if(Number(val) > 10){
					_that.$refs.autoform.formdata.maxScore = 10
				}
				if(Number(val) < 0){
					_that.$refs.autoform.formdata.maxScore = 0
				}
				if( Number(val) >= 0 &&  Number(val)<= 10){
					_that.$refs.autoform.formdata.maxScore = Number(val).toFixed(1)
				}
				if(!val){
					_that.$refs.autoform.clear("title_work","maxScore")
				}
			},
			clearDutyOffId(){
				let _that = this
				_that.$refs.autoform.clear("title_work","dutyOffId")
				_that.clearEvaMultiOffId()
			},
			clearEvaMultiOffId(){
				let _that = this
				_that.$refs.autoform.clear("title_work","evaMultiOffId")
				uni.removeStorage({
					key:_that.defaultDataCache
				})
			},
			getMultiEvaUserResponse(userIdPosId){
				let _that = this
				_that.om.request(
					"workEvaluateConfig/getMultiEvaUserResponse",
					{
						busCode:_that.busCode,
						userIdPosId: userIdPosId
					}
				).then(res=>{
						if(_that.canEdit){
							let objArr = []
							let ids = res.value.evaMultiOffId.split(',')
							let names = res.value.evaMultiOffName.split(',')
							let posNames = res.value.evaMultiPosName.split(',')
							for(let index in ids){
								let obj = {
									checked: true,
									id: null,
									instId: null,
									instName: '',
									isUser: true,
									posId: null,
									posInstName:'',
									posName: '',
									username: ''
								}
								let userPosId = ids[index].split('/')
								obj.id = userPosId[0]
								obj.posId = userPosId[1]
								obj.username = names[index]
								obj.posName = posNames[index]
								objArr.push(obj)
							}
							_that.$refs.autoform.formdata.evaMultiPosName = res.value.evaMultiPosName
							uni.setStorage({
								key:_that.defaultDataCache,
								data:objArr
							})
						}
						
					_that.$refs.autoform.formdata.evaMultiOffId = res.value.evaMultiOffId
					_that.$refs.autoform.formdata.evaMultiOffName = res.value.evaMultiOffName
				})
			},
			verifyScore(){
				let _that = this
				if(Number(_that.$refs.autoform.formdata.maxScore)>_that.maxScore){
					_that.$refs.autoform.clear("title_work","maxScore")
				}
			},
			getMaxScore(edit){
				let _that = this
				_that.om.request(
					"dataDict/getIntByCode",
					{
						code:"DD021017002"
					}
				).then(res=>{
					let editor = _that.$refs.autoform.getEditor('title_work',"maxScore")
					if(!_that.$refs.autoform.formdata.maxScore){
						_that.$refs.autoform.formdata.maxScore = res.value
					}
					_that.maxScore = res.value
				})
			},
			onSelectEndDate(item){
				let _that = this
				let arr = item.split("-")
				let lastDay= new Date(arr[0],arr[1],0).getDate() 
				if((lastDay - Number(arr[2])) < Number(_that.intNum)){
					errorModal(_that,"提示信息","管控参数约束“计划完成日期”可选的最大天数为月末倒数第"+(Number(_that.intNum)+1)+"天，请重新选择！",function(){
						_that.$refs.autoform.clear("title_work","endDate")
					})
				}
			},
			onSelectStartDate(item){
				let _that = this
				let arr = item.split("-")
				let lastDay= new Date(arr[0],arr[1],0).getDate() 
				if((lastDay - Number(arr[2])) < Number(_that.intNum)){
					errorModal(_that,"提示信息","管控参数约束“计划完成日期”可选的最大天数为月末倒数第"+(Number(_that.intNum)+1)+"天，请重新选择！",function(){
						_that.$refs.autoform.clear("title_work","startDate")
					})
				}
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.indexCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexCode = d.orderNum
							uni.removeStorage({
								key:_that.indexCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache,
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let posNameList = []
								for(let index in d){
									idList.push(d[index].id + '/' + d[index].posId)
									nameList.push(d[index].username)
									posNameList.push(d[index].posName)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								let posNameStr = posNameList.map(e =>e).join(",");
								
								formdata.evaMultiOffId = idStr
								formdata.evaMultiOffName = nameStr
								formdata.evaMultiPosName = posNameStr
							}else{
								_that.$refs.autoform.clear("title_work","evaMultiOffId")
							}
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.user1CacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.dutyOffName = d.userPosName
							formdata.dutyOffId = d.id
							formdata.dutyUserId = d.userId
							_that.getMultiEvaUserResponse(d.id+'/'+d.posId)
							uni.removeStorage({
								key:_that.defaultDataCache
							})
							uni.removeStorage({
								key:_that.user1CacheKey
							})
						}
					}
				})
			}
		},
		onUnload() {
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache
			})
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:"dataDict/getIntByCode",
					param:{
						code:'DD021025001'
					}
				}
			]
			subOnLoad(_that, e, function(ajaxData) {
			if(e.configDataCacheKey){
				if(e.busCode){
					_that.busCode = e.busCode
				}
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								let objFormCode = JSON.parse(uni.getStorageSync('objFormCode'))
								changeEditor("title_work","orderNum",{
										defaultName:objFormCode.DICT_CODE_Y+_that.order
								},null,_that.autoformdata)
								let month = ''
								if(e.month.length >1){
									month += e.month
								}else{
									month = '0'+e.month
								}
								let subDay = e.subDay
								let year = moment().format('YYYY')
								let num = moment(year+'-'+month,'YYYY-MM').daysInMonth()
								let day = Number(num) - Number(subDay)
								changeEditor("title_work","endDate","min",`${year}-${month}-${'01'}`,_that.autoformdata)
								changeEditor("title_work","endDate","max",`${year}-${month}-${day}`,_that.autoformdata)
								// changeEditor("title_work","evaMultiOffId",{
								// 	link:"../../public/pageSearch/pageSearch",
								// 	"linkParam": {
								// 		namefield:'userPosName',
								// 		searchfield:"q",
								// 		initload:true,
								// 		url:'orgInstPosUserOfficial/findListByOrgId',
								// 		cachename:_that.userCacheKey,
								// 		placeholder:"请输入姓名或岗位"
								// 	}
								// },null,_that.autoformdata)
								if(ajaxData[0].value + '' == '0'){
									changeEditor("title_work","evaMultiOffId", {
										'link':'../../public/searchUser/searchUser',
										linkParam: {
											cachename: _that.userCacheKey,
											defaultDataCache:_that.defaultDataCache,
											isNew:true,
											isUserPos:true,
											placeholder: urlUtil.userPageUrl().prompt,
											pagination: true
										}
									}, null, _that.autoformdata);
								}else{
									_that.canEdit = false
									changeEditor("title_work","evaMultiOffId",'type','labelbox',_that.autoformdata)
								}
								
								changeEditor("title_work","dutyOffId",{
									link:"../../public/pageSearch/pageSearch",
									"linkParam": {
										other:JSON.stringify({
											userId:e.userId
										}),
										namefield:urlUtil.userPageUrl().namefield,
										searchfield:"q",
										initload:true,
										url:"orgInstPosUserOfficial/findListByUserIdOrgId",
										cachename:_that.user1CacheKey,
										placeholder:"请输入姓名或岗位"
									}
								},null,_that.autoformdata)
								changeEditor("title_work","indexCode",{
									link:"../../public/pageSearch/pageSearch",
									"linkParam": {
										other:JSON.stringify({
										}),
										namefield:"indexName",
										notefield:"busRange",
										defaultData:e.dataList,
										initload:true,
										cachename:_that.indexCacheKey,
									}
								},null,_that.autoformdata)
								if(_that.formdata.evaMultiOffId && (ajaxData[0].value + '' == '0')){
									let objArr = []
									let ids = _that.formdata.evaMultiOffId.split(',')
									let names = _that.formdata.evaMultiOffName.split(',')
									let posNames = _that.formdata.evaMultiPosName.split(',')
									for(let index in ids){
										let obj = {
											checked: true,
											id: null,
											instId: null,
											instName: '',
											isUser: true,
											posId: null,
											posInstName:'',
											posName: '',
											username: ''
										}
										let userPosId = ids[index].split('/')
										obj.id = userPosId[0]
										obj.posId = userPosId[1]
										obj.username = names[index]
										obj.posName = posNames[index]
										objArr.push(obj)
									}
									uni.setStorage({
										key:_that.defaultDataCache,
										data:objArr
									})
								}
								if(_that.formdata.upperFlag){
									_that.deleteBtn = false
									changeEditor("title_work",'workName','type','labelbox',_that.autoformdata)
									setTimeout(()=>{
										_that.om.toast('当前工作项为上级分解工作项，禁止修改工作项名称',2000)
									},500)
								}
								
							}
						}
					})
				}
				_that.$nextTick(function(){
					_that.getMaxScore(e.edit)
				})				
			},arr)
		}
	}
</script>

<style>
</style>